#include <mpuiic.h>
int mpuiic_init(uint8_t slave_addr){
    int ret;
    ret += fpioa_set_function(30,FUNC_I2C0_SDA);
    ret += fpioa_set_function(31,FUNC_I2C0_SCLK);
    i2c_init(I2C_DEVICE_0,slave_addr,7,400000);
    msleep(300);
    return ret;
}
int mpuiic_read_len(uint8_t slave_addr, uint8_t reg_addr,uint8_t length, uint8_t *data){
    int ret = 0;
    ret = i2c_recv_data(I2C_DEVICE_0, &reg_addr, 1, data, length);
    return ret;
}
int mpuiic_write_len(uint8_t slave_addr, uint8_t reg_addr,uint8_t length, uint8_t *data){
    int ret = 0;
    uint8_t *buf = malloc(length + 1);
    buf[0] = reg_addr;
    memcpy(buf + 1, data, length);
    ret = i2c_send_data(I2C_DEVICE_0, buf, length + 1);
    free(buf);
    return ret;
}
uint8_t mpuiic_read(uint8_t slave_addr, uint8_t reg_addr){
    uint8_t data=0;
    i2c_recv_data(I2C_DEVICE_0, &reg_addr, 1, &data, 1);
    return data;
}
int mpuiic_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t data){
    int ret = 0;
    uint8_t *buf = malloc(2);
    buf[0] = reg_addr;
    buf[1] = data;
    ret = i2c_send_data(I2C_DEVICE_0, buf, 2);
    free(buf);
    return ret;
}